home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / Sample Code / Sample Editors⁄Viewers / Panel Editor / Source / PrinterQD.cpp < prev    next >
Encoding:
Text File  |  1995-12-08  |  9.6 KB  |  409 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PrinterQD.cpp
  3.  
  4.     Contains:    QuickDraw (classic) printing support for OpenDoc printing utility
  5.  
  6.     Written by:    Steve Smith, Thomas Weisbach, Jens Alfke
  7.  
  8.     Copyright:    © 1995 by Apple Computer, Inc., all rights reserved.
  9. */
  10.  
  11.  
  12. // -- OpenDoc --
  13.  
  14. #ifndef _ALTPOINT_
  15. #include <AltPoint.h>
  16. #endif
  17.  
  18. #ifndef SOM_ODCanvas_xh
  19. #include <Canvas.xh>
  20. #endif
  21.  
  22. #ifndef SOM_ODClipboard_xh
  23. #include <Clipbd.xh>
  24. #endif
  25.  
  26. #ifndef SOM_Module_OpenDoc_Commands_defined
  27. #include <CmdDefs.xh>
  28. #endif
  29.  
  30. #ifndef SOM_ODDispatcher_xh
  31. #include <Disptch.xh>
  32. #endif
  33.  
  34. #ifndef SOM_ODFacet_xh
  35. #include <Facet.xh>
  36. #endif
  37.  
  38. #ifndef SOM_ODFrame_xh
  39. #include <Frame.xh>
  40. #endif
  41.  
  42. #ifndef SOM_ODMenuBar_xh
  43. #include <MenuBar.xh>
  44. #endif
  45.  
  46. #ifndef SOM_ODSession_xh
  47. #include <ODSessn.xh>
  48. #endif
  49.  
  50. #ifndef SOM_ODStorageSystem_xh
  51. #include <ODStor.xh>
  52. #endif
  53.  
  54. #ifndef SOM_ODPart_xh
  55. #include <Part.xh>
  56. #endif
  57.  
  58. #ifndef SOM_ODPlatformTypeList_xh
  59. #include <PfTypLs.xh>
  60. #endif
  61.  
  62. #ifndef SOM_ODShape_xh
  63. #include <Shape.xh>
  64. #endif
  65.  
  66. #ifndef SOM_Module_OpenDoc_StdProps_defined
  67. #include <StdProps.xh>
  68. #endif
  69.  
  70. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  71. #include <StdTypes.xh>
  72. #endif
  73.  
  74. #ifndef SOM_ODTransform_xh
  75. #include <Trnsform.xh>
  76. #endif
  77.  
  78. #ifndef SOM_ODWindowState_xh
  79. #include <WinStat.xh>
  80. #endif
  81.  
  82. // -- OpenDoc Utilities --
  83.  
  84. #ifndef _EXCEPT_
  85. #include <Except.h>
  86. #endif
  87.  
  88. #ifndef _ITEXT_
  89. #include <IText.h>
  90. #endif
  91.  
  92. #ifndef _DLOGUTIL_
  93. #include <DlogUtil.h>
  94. #endif
  95.  
  96. #ifndef _ODDEBUG_
  97. #include <ODDebug.h>
  98. #endif
  99.  
  100. #ifndef _ODNEW_
  101. #include <ODNew.h>
  102. #endif
  103.  
  104. #ifndef _ODUTILS_
  105. #include <ODUtils.h>
  106. #endif
  107.  
  108. #ifndef _STORUTIL_
  109. #include <StorUtil.h>
  110. #endif
  111.  
  112. #ifndef _TEMPOBJ_
  113. #include <TempObj.h>
  114. #endif
  115.  
  116. #ifndef _USERSRCM_
  117. #include <UseRsrcM.h>
  118. #endif
  119.  
  120. // -- MacToolbox --
  121.  
  122. #ifndef __TEXTUTILS__
  123. #include <TextUtils.h>
  124. #endif
  125.  
  126. // -- Printer --
  127. #define _PRINTER_PRIVATE_
  128. #ifndef _PRINTER_
  129. #include "Printer.h"
  130. #endif
  131.  
  132. #pragma segment Printer
  133.  
  134.  
  135. //---------------------------------------------------------------------------------
  136. // THROW_IF_PRINT_ERROR  [static]
  137. //---------------------------------------------------------------------------------
  138.  
  139. static void
  140. THROW_IF_PRINT_ERROR( )
  141. {
  142.     OSErr err = PrError();
  143.     if( err ) THROW(err);
  144. }
  145.  
  146.  
  147. //=================================================================================
  148. // CQDPrinter implementation
  149. //=================================================================================
  150.  
  151. //---------------------------------------------------------------------------------
  152. // CQDPrinter::CQDPrinter
  153. //---------------------------------------------------------------------------------
  154.  
  155. CQDPrinter::CQDPrinter()
  156. {
  157.     fJob = kODNULL;
  158.     fPort = kODNULL;
  159.     fIOBuffer = kODNULL;
  160.     fPrintingOpen = 0;
  161.     fPageOpen = kODFalse;
  162. }
  163.  
  164. //---------------------------------------------------------------------------------
  165. // CQDPrinter::~CQDPrinter
  166. //---------------------------------------------------------------------------------
  167.  
  168. CQDPrinter::~CQDPrinter()
  169. {
  170.     ODDisposeHandle((ODHandle)fJob);
  171.     ODDisposePtr(fPort);                    // Should have been disposed earlier, but...
  172.     ODDisposePtr(fIOBuffer);
  173. }
  174.  
  175. //---------------------------------------------------------------------------------
  176. // CQDPrinter::OpenPrinting
  177. //---------------------------------------------------------------------------------
  178.  
  179. void CQDPrinter::OpenPrinting()
  180. {
  181.     PrOpen();
  182.     THROW_IF_PRINT_ERROR();
  183.     inherited::OpenPrinting();
  184. }
  185.  
  186. //---------------------------------------------------------------------------------
  187. // CQDPrinter::ClosePrinting
  188. //---------------------------------------------------------------------------------
  189.  
  190. void CQDPrinter::ClosePrinting()
  191. {
  192.     PrClose();
  193.     inherited::ClosePrinting();
  194. }
  195.  
  196. //---------------------------------------------------------------------------------
  197. // CQDPrinter::ReadJobFromHandle
  198. //---------------------------------------------------------------------------------
  199.  
  200. ODPlatformPrintJob CQDPrinter::ReadJobFromHandle(ODValueType valueType, ODHandle h)
  201. {
  202.     ASSERT(valueType==kODTypeQuickDrawPageSetup,kODErrInvalidGraphicsSystem);
  203.     WASSERT(fJob==kODNULL);
  204.     
  205.     fJob = (THPrint) h;
  206.     return (ODPlatformPrintJob)fJob;
  207. }
  208.  
  209. //---------------------------------------------------------------------------------
  210. // CQDPrinter::CreateNewJob
  211. //---------------------------------------------------------------------------------
  212.  
  213. ODPlatformPrintJob CQDPrinter::CreateNewJob()
  214. {
  215.     fJob = (THPrint) ODNewHandle(sizeof(TPrint));
  216.     PrintDefault(fJob);
  217.     if( PrError() ) {
  218.         ODDisposeHandle((ODHandle)fJob);
  219.         fJob = kODNULL;
  220.         THROW(PrError());
  221.     }
  222.     return (ODPlatformPrintJob)fJob;
  223. }
  224.  
  225. //---------------------------------------------------------------------------------
  226. // CQDPrinter::CopyJobToHandle
  227. //---------------------------------------------------------------------------------
  228.  
  229. ODHandle CQDPrinter::CopyJobToHandle( )
  230. {
  231.     ASSERT(fJob!=kODNULL,kODErrAssertionFailed);
  232.     return ODCopyHandle((ODHandle)fJob);
  233. }
  234.  
  235. //---------------------------------------------------------------------------------
  236. // CQDPrinter::CreatePrintingPlatformCanvas
  237. //---------------------------------------------------------------------------------
  238.  
  239. ODPlatformCanvas CQDPrinter::CreatePrintingPlatformCanvas( Environment*, ODGraphicsSystem g,
  240.                                                             ODFrame*, ODShape* )
  241. {
  242.     ASSERT(g==kODQuickDraw,kODErrInvalidGraphicsSystem);
  243.     WASSERT(!fPort);
  244.     WASSERT(!fIOBuffer);
  245.     
  246.     fPort = (TPrPort*) ODNewPtr(sizeof(TPrPort));
  247.     fIOBuffer = (Ptr) ODNewPtr(522);
  248.     
  249.     PrOpenDoc(fJob, fPort, fIOBuffer);
  250.     if( PrError() ) {
  251.         ODDisposePtr(fPort);
  252.         ODDisposePtr(fIOBuffer);
  253.         fPort = kODNULL;
  254.         fIOBuffer = kODNULL;
  255.         THROW(PrError());
  256.     }
  257.     return (ODPlatformCanvas) fPort;
  258. }
  259.  
  260. //---------------------------------------------------------------------------------
  261. // CQDPrinter::CleanupPrintingEnv
  262. //---------------------------------------------------------------------------------
  263.  
  264. void CQDPrinter::CleanupPrintingEnv(Environment* ev, ODFrame* initiator)
  265. {
  266.     // This routine should not throw exeptions and must call the inherited method.
  267.     
  268.     if( fPort ) {
  269.         TRY{
  270.             this->ClosePage(ev);                // In case it was left open
  271.         }CATCH_ALL{
  272.         }ENDTRY
  273.         
  274.         PrCloseDoc(fPort);
  275.         
  276.         // Spool document if necessary for this printer:
  277.         if( (**fJob).prJob.bJDocLoop == bSpoolLoop && PrError() == noErr ) {
  278.             TPrStatus theStatus;
  279.             PrPicFile(fJob, fPort, fIOBuffer, kODNULL, &theStatus);
  280.         }
  281.         
  282.         ODDisposePtr(fIOBuffer);
  283.         ODDisposePtr(fPort);
  284.         fPort = kODNULL;
  285.         fIOBuffer = kODNULL;
  286.     }
  287.     
  288.     inherited::CleanupPrintingEnv(ev,initiator);
  289. }
  290.  
  291. //---------------------------------------------------------------------------------
  292. // CQDPrinter::GetPageRect
  293. //---------------------------------------------------------------------------------
  294.  
  295. ODRect CQDPrinter::GetPageRect( Environment *ev )
  296. {
  297.     this->GetPlatformPrintJob(ev);        // Force internalization of fJob
  298.     
  299.     return (ODRect) (**fJob).prInfo.rPage;
  300. }
  301.  
  302. //---------------------------------------------------------------------------------
  303. // CQDPrinter::OpenPage
  304. //---------------------------------------------------------------------------------
  305.  
  306. void CQDPrinter::OpenPage( Environment*, ODUShort page )
  307. {
  308.     if( !fPageOpen ) {
  309.         PrOpenPage(fPort, kODNULL);
  310.         THROW_IF_PRINT_ERROR();
  311.         fPageOpen = kODTrue;
  312.     }
  313. }
  314.  
  315. //---------------------------------------------------------------------------------
  316. // CQDPrinter::ClosePage
  317. //---------------------------------------------------------------------------------
  318.  
  319. void CQDPrinter::ClosePage( Environment* )
  320. {
  321.     if( fPageOpen ) {
  322.         PrClosePage(fPort);
  323.         fPageOpen = kODFalse;
  324.         THROW_IF_PRINT_ERROR();
  325.     }
  326. }
  327.  
  328. //---------------------------------------------------------------------------------
  329. // CQDPrinter::RunPageSetupDialog
  330. //---------------------------------------------------------------------------------
  331.  
  332. ODBoolean CQDPrinter::RunPageSetupDialog( Environment* )
  333. {
  334.     PrValidate(fJob);
  335.     THROW_IF_PRINT_ERROR();
  336.     
  337.     ODBoolean success = PrStlDialog(fJob);
  338.     THROW_IF_PRINT_ERROR();
  339.     return success;
  340. }
  341.  
  342. //---------------------------------------------------------------------------------
  343. // CQDPrinter::RunPrintDialog
  344. //---------------------------------------------------------------------------------
  345.  
  346. ODBoolean CQDPrinter::RunPrintDialog( Environment* )
  347. {
  348.     ODBoolean success;
  349.     TRY{
  350.         ASSERT(fJob!=kODNULL,kODErrAssertionFailed);
  351.         
  352.         PrValidate(fJob);
  353.         THROW_IF_PRINT_ERROR();
  354.  
  355.         success = PrJobDialog(fJob);
  356.         THROW_IF_PRINT_ERROR();
  357.         
  358.     }CATCH_ALL{
  359.         WARN("Error %d in page setup dialog",ErrorCode());
  360.         success = kODFalse;
  361.         // ignore exception
  362.     }ENDTRY
  363.     return success;
  364. }
  365.  
  366. //---------------------------------------------------------------------------------
  367. // CQDPrinter::PrintDocument
  368. //---------------------------------------------------------------------------------
  369.  
  370. void CQDPrinter::DoPrint(Environment* ev, ODShape* area)
  371. {
  372.     // Here's what you've been waiting for: the print loop.
  373.     // See Tech Note PR10 "A Printing Loop That Cares" for more details.
  374.     
  375.     // Extract page range from print job:
  376.     short firstPage = (**fJob).prJob.iFstPage;
  377.     short lastPage = (**fJob).prJob.iLstPage;
  378.     ODULong pageCount = this->CountPages(ev, area);
  379.     if ( lastPage > pageCount ) 
  380.         lastPage = pageCount;
  381.     (**fJob).prJob.iFstPage = 1;
  382.     (**fJob).prJob.iLstPage = iPrPgMax;
  383.  
  384.     // Print the whole thing as many times as the user wants.
  385.     short totalPages = 1;
  386.     for( short copy = (**fJob).prJob.iCopies; copy>0 ; copy-- )
  387.     {
  388.         // Print the pages, one at a time.
  389.         for (short page = firstPage; page <= lastPage; page++, totalPages++)
  390.         {
  391.             if (totalPages % iPFMaxPgs == 0)
  392.             {
  393.                 // Every 128 pages we must close, spool, and re-open printing:
  394.                 PrCloseDoc(fPort);
  395.                 if( (**fJob).prJob.bJDocLoop == bSpoolLoop && PrError() == noErr ) {
  396.                     TPrStatus theStatus;
  397.                     PrPicFile(fJob, fPort, fIOBuffer, kODNULL, &theStatus);
  398.                 }
  399.                 PrOpenDoc(fJob, fPort, (Ptr)fIOBuffer);
  400.                 THROW_IF_PRINT_ERROR();
  401.             }
  402.             
  403.             this->PrintPage(ev, page, area);                // Shazam!
  404.         
  405.             THROW_IF_PRINT_ERROR();
  406.         }
  407.     }
  408. }
  409.